package com.sidneysimmons.financially.fragments;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.sidneysimmons.financially.R;

public class AccountActionsFragment extends DialogFragment {
	
	private final String CLASS_NAME = this.getClass().getName();
	
	/** Used by the FragmentManager to identify this fragment. */
	public static String TAG = "account_actions_fragment";
	
	private Activity context;
	
	public static AccountActionsFragment newInstance() {
		AccountActionsFragment fragment = new AccountActionsFragment();
		
		/* // We could dynamically put data into the dialog here
        Bundle bundle = new Bundle();
        bundle.putInt("title", title);
        fragment.setArguments(bundle); */
		
		return fragment;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Use this to get dynamically put data, everything is static now
        // int title = getArguments().getInt("title");

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setItems(R.array.account_actions, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				FragmentManager fragmentManager = null;
				try {
					fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
				} catch (ClassCastException e) {
					Log.e(CLASS_NAME, "Exception thrown while casting 'context' in AccountActionsFragment.java to FragmentActivity.");
					Log.e(CLASS_NAME, "Not executing any Account Action.");
					return;
				}
				
	    		// Split off here - depending on which action was clicked
	    		if (which == 0) {
	    			
	    		} else if (which == 1) {
	    			NewCategoryDialogFragment newCategoryDialogFragment = NewCategoryDialogFragment.newInstance();
	    			newCategoryDialogFragment.show(fragmentManager, NewCategoryDialogFragment.TAG);
	    		} else if (which == 2) {
	    			EditAccountDialogFragment editAccountDialogFragment = EditAccountDialogFragment.newInstance();
	    			editAccountDialogFragment.show(fragmentManager, EditAccountDialogFragment.TAG);
	    		}
	    	}
	    });
	    
	    return builder.create();
	}
	
	/**
	 * Called when the Fragment and it's Activity are attached.
	 */
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // Set this as the context to use later
        context = activity;
    }
}